/*
 * ServiceLocator.java
 *
 * Created on 22 de agosto de 2005, 05:14 PM
 */
package web;


import java.net.URL;
import java.sql.Connection;
import java.sql.SQLException;
import javax.naming.Context;


import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

/**
 *
 * @author edjaiv
 * @version
 */
public class ServiceLocator {
    
    private InitialContext ic;
    
    public ServiceLocator() {
        try {
            ic = new InitialContext();
        } catch (NamingException ne) {
            throw new RuntimeException(ne);
        }
    }
    
    private Object lookup(String jndiName) throws NamingException {
        return ic.lookup(jndiName);
    }
    

    /**
     * This method obtains the datasource itself for a caller
     * @return the DataSource corresponding to the name parameter
     */
    public DataSource getDataSource(String dataSourceName) throws NamingException {

        return (DataSource) lookup(dataSourceName);
    }
    
    /**
     * @return the URL value corresponding
     * to the env entry name.
     */
    public URL getUrl(String envName) throws NamingException {
        return (URL) lookup(envName);
    }
    
    /**
     * @return the boolean value corresponding
     * to the env entry
     */
    public boolean getBoolean(String envName) throws NamingException {
        Boolean bool = (Boolean) lookup(envName);
        return bool.booleanValue();
    }
    
    /** 
     * @return the String value corresponding
     * to the env entry name.
     */
    public String getString(String envName) throws NamingException {
        return (String) lookup(envName);
    }
    
    public Connection getConexion(String datasource) throws NamingException, SQLException{

            Context ctx = new InitialContext();
            DataSource ds = (DataSource)ctx.lookup("java:/"+datasource);
            return ds.getConnection();

    }
        
}

